package com.search.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.base.core.pager.PageBean;
import com.base.core.pager.PageContext;
import com.search.dao.repo.ItemRepository;
import com.search.entity.Item;
import com.search.service.SearchItemService;
import com.search.service.dto.SearchItem;
import com.search.service.dto.SearchItemParam;
import org.apache.dubbo.config.annotation.DubboService;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@DubboService(version = "0.0.1-alpha", timeout = 3000, delay = -1)
public class SearchItemServiceImpl implements SearchItemService {

    @Resource
    private ItemRepository productRepository;

    private final String DESC = "-1";
    private final String ASC = "1";

    @Override
    public PageBean<SearchItem> search(SearchItemParam searchItemParam, int page, int size, String sort) {
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        BoolQueryBuilder booleanQueryBuilder = new BoolQueryBuilder();
        if (ObjectUtil.isNotEmpty(searchItemParam.getStoreId())) {
            booleanQueryBuilder.must(QueryBuilders.termQuery("storeId", searchItemParam.getStoreId().toString()));
        }
        if (ObjectUtil.isNotEmpty(searchItemParam.getName())) {
            booleanQueryBuilder.must(QueryBuilders.matchPhrasePrefixQuery("name", searchItemParam.getName()));
        }

        if (DESC.equals(sort)) {
            builder.withSort(new FieldSortBuilder("salePrice").unmappedType("float").order(SortOrder.DESC));
        } else if (ASC.equals(sort)) {
            builder.withSort(new FieldSortBuilder("salePrice").unmappedType("float").order(SortOrder.ASC));
        } else {
            builder.withSort(SortBuilders.scoreSort().order(SortOrder.DESC));
        }

        SearchQuery searchQuery = builder
                .withQuery(booleanQueryBuilder)
                .withPageable(PageRequest.of(page-1, size))
                .build();

        Page<Item> result = productRepository.search(searchQuery);
        List<SearchItem> list = result.getContent().stream().map(product -> {
            SearchItem searchItem = new SearchItem();
            searchItem.setDrugsId(product.getId());
            searchItem.setStoreId(product.getStoreId());
            searchItem.setName(product.getName());
            searchItem.setSalePrice(product.getSalePrice());
            searchItem.setSpecs(product.getSpecs());
            searchItem.setCategoryId(product.getCategoryId());
            searchItem.setDrugsNo(product.getDrugsNo());
            searchItem.setQuantity(product.getQuantity());
            return searchItem;
        }).collect(Collectors.toList());
        Long count = result.getTotalElements();
        PageContext.setDataCount(count);
        PageContext.setPageSize(new Long(size));
        PageContext.setCurrentPage(new Long(page));
        PageBean<SearchItem> searchItemPageBean = new PageBean<>(list);
        PageContext.clear();
        return searchItemPageBean;
    }


}
